// 点击去登录
qs('.register .mb-3').onclick = function () {
  qs('.login').style.display = 'block'
  qs('.register').style.display = 'none'
}

// 点击去注册
qs('.login .mb-3').onclick = function () {
  qs('.login').style.display = 'none'
  qs('.register').style.display = 'block'
}

// 注册功能
qs('.register form').onsubmit = function (e) {
  e.preventDefault()
  const username = qs('.register [name=username]').value.trim()
  const password = qs('.register [name=password]').value.trim()
  // console.log(username,password);
  if (!username || !password) return alert('账号或密码不能为空')

  const data = `username=${username}&password=${password}`
  axios.post('/api/register', data).then(({data : {code, message}}) => {
    console.log(code, message)
    if (code === 0) {
      alert(message)
    }
  })
}

// 登录功能
qs('.login form').onsubmit = function (e) {
  e.preventDefault()
  const username = qs('.login [name=username]').value.trim()
  const password = qs('.login [name=password]').value.trim()
  // console.log(username,password);
  if (!username || !password) return alert('账号或密码不能为空')

  const data = `username=${username}&password=${password}`
  axios.post('/api/login', data).then(({data: {code, message, token}}) => {
    console.log(code, message, token)
    if (code === 0) {
      alert(message)
      localStorage.setItem('token', token)
      location.href = './index.html'
    }
  })
}